<?php

	class XmlParser {

		private $_xmlString = '';
		private $_xmlStringArray = array();

		public function __construct() {}

		public function parseUrl( $url ) {
			return $this->parseFile( $url );
		}

		public function parseFile( $fileName ) {
			if( $content = file_get_contents( $fileName ) ) {
				return $this->parseString( $content );
			}
		}

		public function parseString( $string ) {
			$this->_xmlString = '<xmlstring>' . $string . '</xmlstring>';
			$xml = simplexml_load_string( $string, null, LIBXML_NOCDATA | LIBXML_NOBLANKS );
			if( $xml !== false ) {
				$this->_xmlStringArray = $this->_processArray( json_decode( json_encode( $xml ), true ) );
			}
			return $this->_xmlStringArray;
		}

		public function getResult() {
			return $this->_xmlStringArray;
		}

		private function _processArray( $arr ) {
			if( !$arr ) {
				return array();
			}
			$resultingArray = array();
			foreach( $arr as $key => $value ) {
				if( is_array( $value ) ) {
					$resultingArray[ lcfirst( str_replace( '@', '', $key ) ) ] = $this->_processArray( $value );
				} else {
					$resultingArray[ lcfirst( str_replace( '@', '', $key ) ) ] = $value;
				}
			}
			return $resultingArray;
		}

		private function _implode( $glue, $arr ) {
			if( !is_array( $arr ) ) {
				return trim( $arr );
			}
			foreach( $arr as $key => $value ) {
				if( is_array( $value ) ) {
					$arr[ $key ] = trim( $this->_implode( $glue, $value ) );
				} else {
					$arr[ $key ] = trim( $value );
				}
			}
			return trim( implode( $glue, $arr ) );
		}

		public function getNewsData() {
			$newsml = $this->_xmlStringArray;
			$result = array();
			foreach( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'newsLines' ] as $key => $value ) {
				if( !$this->_implode( ' ', $value ) ) {
					continue;
				}
				if( array_search( $key, array( 'copyrightLine', 'newsLine' ) ) === false ) {
					$key = substr( $key, 0, strlen( $key ) - 4 );
					$key = $key == 'head' ? 'title' : $key;
					$result[ $key ] = $this->_implode( ' ', $value );
				}
			}
			if( isset( $newsml[ 'newsEnvelope' ][ 'transmissionId' ] ) ) {
				$result[ 'transmissionId' ] = $newsml[ 'newsEnvelope' ][ 'transmissionId' ];
			}
			$result[ 'newsItemId' ] = $newsml[ 'newsItem' ][ 'identification' ][ 'newsIdentifier' ][ 'newsItemId' ];
			$result[ 'dateLabel' ] = $newsml[ 'newsItem' ][ 'identification' ][ 'dateLabel' ];
			if( isset( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'descriptiveMetadata' ][ 'property' ] ) ) {
				foreach( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'descriptiveMetadata' ][ 'property' ] as $property ) {
					$result[ lcfirst( $property[ 'attributes' ][ 'formalName' ] ) ] = $property[ 'attributes' ][ 'value' ];
				}
			}
			if( isset( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'newsComponent' ] ) ) {
				foreach( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'newsComponent' ] as $i => $component ) {
					if( $component[ 'role' ][ 'attributes' ][ 'formalName' ] != 'ImageClip' ) {
						$result[ $component[ 'role' ][ 'attributes' ][ 'formalName' ] ] = $component[ 'contentItem' ][ 'dataContent' ];
					} else {
						$image = array();
						foreach( $component[ 'newsItem' ][ 'newsComponent' ][ 'newsLines' ] as $key => $value ) {
							if( !$this->_implode( ' ', $value ) ) {
								continue;
							}
							if( array_search( $key, array( 'copyrightLine', 'newsLine' ) ) === false ) {
								$key = substr( $key, 0, strlen( $key ) - 4 );
								$key = $key == 'head' ? 'title' : $key;
								$image[ $key ] = $this->_implode( ' ', $value );
							} else {
								foreach( $value as $newsline ) {
									$image[ lcfirst( $newsline[ 'newsLineType' ][ 'attributes' ][ 'formalName' ] ) ] = $newsline[ 'newsLineText' ];
								}
							}
						}
						$fileInfo = $component[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'contentItem' ];
						$image[ 'file' ] = $fileInfo[ 'attributes' ][ 'href' ];
						$image[ 'size' ] = $fileInfo[ 'characteristics' ][ 'sizeInBytes' ];
						foreach( $fileInfo[ 'characteristics' ][ 'property' ] as $property ) {
							$image[ lcfirst( $property[ 'attributes' ][ 'formalName' ] ) ] = $property[ 'attributes' ][ 'value' ];
						}
						$result[ 'images' ][] = $image;
					}
				}
			}
			if( isset( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'newsLines' ][ 'newsLine' ] ) ) {
				$result[ lcfirst( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'newsLines' ][ 'newsLine' ][ 'newsLineType' ][ 'attributes' ][ 'formalName' ] ) ] = $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'newsLines' ][ 'newsLine' ][ 'newsLineText' ];
			}
			if( isset( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'contentItem' ] ) ) {
				$result[ 'content' ] = '<p>' . $this->_implode( "</p>\n<p>", $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'contentItem' ][ 'dataContent' ][ 'html' ][ 'body' ] ) . '</p>';
			}
			if( isset( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'topicSet' ] ) ) {
				foreach( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'topicSet' ][ 'topic'] as $i => $topic ) {
					$result[ 'topics' ][] = array(
						'type' => $topic[ 'topicType' ][ 'attributes' ][ 'formalName' ],
						'name' => is_array( $topic[ 'formalName' ] ) ? $topic[ 'formalName' ][ 0 ] : $topic[ 'formalName' ],
						'description' => $topic[ 'description' ],
						'whyPresent' => $topic[ 'property' ][ 'attributes' ][ 'value' ]
					);
				}
			}
			if( isset( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'contentItem' ] ) ) {
				foreach( $newsml[ 'newsItem' ][ 'newsComponent' ][ 'newsComponent' ][ 'contentItem' ][ 'characteristics' ][ 'property' ] as $i => $property ) {
					$result[ 'properties' ][ $property[ 'attributes' ][ 'formalName' ] ] = $property[ 'attributes' ][ 'value' ];
				}
			}
			return $result;
		}
	}